package cn.livingCloud.entity.user;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.repository.AccountPrivilegeMapper;
import cn.livingCloud.repository.UserAccountMapper;
import cn.livingCloud.repository.UserMapper;
import cn.livingCloud.utils.SessionUtils;
import cn.livingCloud.utils.UuidGenerator;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.SimpleAuthenticationInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserAccount {
    public static final String REALM_NAME = "UserAccount";
    private transient UserMapper userMapper = BeanProvider.getBean(UserMapper.class);
    private transient UserAccountMapper userAccountMapper = BeanProvider.getBean(UserAccountMapper.class);
    private transient AccountPrivilegeMapper privilegeMapper = BeanProvider.getBean(AccountPrivilegeMapper.class);
    private String uuid = UuidGenerator.generate();
    private String userId;
    private User user;
    //员工id
    private String employeeUuid;

    private String username;
    private String password;

    /**
     * 是否启用
     * true:启用,false:禁用
     */
    private boolean enable;
    /**
     * 最后登录时间
     * yyyy-MM-dd HH:mm:ss
     */
    private Date lastLoginTime;

    private List<AccountPrivilege> privilegeList = new ArrayList<AccountPrivilege>();

    public boolean isEnable() {
        return enable;
    }

    public UserAccount setEnable(boolean enable) {
        this.enable = enable;
        return this;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public UserAccount setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
        return this;
    }

    public String getUsername() {
        return username;
    }

    public UserAccount setUsername(String username) {
        this.username = username;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public UserAccount setPassword(String password) {
        this.password = password;
        return this;
    }

    public String getUuid() {
        return uuid;
    }

    public UserAccount setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getUserId() {
        return userId;
    }

    public UserAccount setUserId(String userId) {
        this.userId = userId;
        return this;
    }

    public String getEmployeeUuid() {
        return employeeUuid;
    }

    public UserAccount setEmployeeUuid(String employeeUuid) {
        this.employeeUuid = employeeUuid;
        return this;
    }

    public List<AccountPrivilege> getPrivilegeList() {
        return privilegeList;
    }

    public UserAccount setPrivilegeList(List<AccountPrivilege> privilegeList) {
        this.privilegeList = privilegeList;
        return this;
    }

    /**
     * 获取账号的权限
     *
     * @return 权限列表
     */
    public List<AccountPrivilege> getAccountPrivilegeList() {
        return privilegeMapper.findAccountPrivilegeList(this.uuid);
    }

    public User getUser() {
        return user;
    }

    public UserAccount setUser(User user) {
        this.user = user;
        return this;
    }

    public void addPrivilege(AccountPrivilege accountPrivilege) {
        this.privilegeList.add(accountPrivilege);
    }

    public UserAccount save() {
        userMapper.saveUserAccount(this);
        return this;
    }

    public UserAccount save(String userId, String name, String username, String password) {
        this.setUuid(uuid);
        this.setUserId(userId);
        this.setUsername(username);
        this.setPassword(password);
        userMapper.saveUserAccount(this);
        return this;
    }

    public void update() {
        userAccountMapper.update(this);
    }

    public AuthenticationInfo login() {
        this.setLastLoginTime(new Date());
        userAccountMapper.update(this);
        SessionUtils.setAttribute(SessionConstants.USER_KEY, user);
        SessionUtils.setAttribute(SessionConstants.USER_ACCOUNT_KEY, this);

        return new SimpleAuthenticationInfo(this.username, this.password, REALM_NAME);
    }


    public String getRole() {
        return "USER";
    }

    @Override
    public String toString() {
        return "UserAccount{" +
                "employeeUuid='" + employeeUuid + '\'' +
                ", userMapper=" + userMapper +
                ", userAccountMapper=" + userAccountMapper +
                ", privilegeMapper=" + privilegeMapper +
                ", uuid='" + uuid + '\'' +
                ", userId='" + userId + '\'' +
                ", user=" + user +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", enable=" + enable +
                ", lastLoginTime=" + lastLoginTime +
                ", privilegeList=" + privilegeList +
                '}';
    }
}
